package com.app.controller.backstage;

import com.app.common.Result;
import com.app.mapper.CategoryMapper;
import com.app.model.AdminUserModel;
import com.app.model.CategoryModel;
import com.app.model.ComplaintModel;
import com.app.service.AdminUserService;
import com.app.service.BrowseNumberService;
import com.app.service.ComplaintService;
import com.app.service.UserService;
import com.app.util.TokenUtils;
import com.app.vo.CensusVo;
import com.app.vo.MsgVo;
import com.app.vo.ValueAndNameVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@Controller
@RequestMapping("/jtwq")
public class CensusBehindController {
    @Autowired
    private UserService userService;
    @Autowired
    private ComplaintService complaintService;
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private BrowseNumberService browseNumberService;

    /**
     * 统计数量
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getBehindCensus")
    public Result<Object> getBehindCensus(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        CensusVo vo = new CensusVo();
        //投诉总数
        int allCount = complaintService.selectAllCount(user);
        //已办结总数
        int statusCount = complaintService.selectStatusCount(user);
        //统计黄灯和红灯数量
        Integer noGreenLightCount = complaintService.selectNoGreenLightCount(user);
        //用户总数
        int userAllCount = userService.selectAllCount();
        //用户访问量
        int userCount = browseNumberService.selectCount();
        vo.setUserAllCount(userAllCount);
        vo.setUserCount(userCount);
        vo.setNoGreenLight(noGreenLightCount);
        vo.setAllCount(allCount);
        vo.setStatusCount(statusCount);
        vo.setRoles(user.getRoles());
        return Result.success(vo);
    }


    //饼图
    @GetMapping("/getBehindPie")
    public Result<Object> getBehindPie(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        MsgVo vo = new MsgVo();
        vo.setName("建设领域");
        vo.setValue(complaintService.selectByType(1,user.getId(),user.getRoles()));
        MsgVo vo1 = new MsgVo();
        vo1.setName("非建设领域");
        vo1.setValue(complaintService.selectByType(2,user.getId(),user.getRoles()));
        MsgVo vo3 = new MsgVo();
        vo3.setName("待办理");
        vo3.setValue(complaintService.selectByStatus(1,user.getId(),user.getRoles()));
        MsgVo vo4 = new MsgVo();
        vo4.setName("已办结");
        vo4.setValue(complaintService.selectByStatus(2,user.getId(),user.getRoles()));
        MsgVo vo6 = new MsgVo();
        vo6.setName("处理中");
        vo6.setValue(complaintService.selectByStatus(4,user.getId(),user.getRoles()));
        List<MsgVo> voList = new ArrayList<>();
        voList.add(vo);
        voList.add(vo1);
        voList.add(vo3);
        voList.add(vo4);
//        voList.add(vo5);
        voList.add(vo6);
        return Result.success(voList);
    }

    //行业柱状图
    @GetMapping("/industryStatistics")
    public Result<Object> industryStatistics(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        List<ValueAndNameVo> list = new ArrayList<>();
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 10);
        List<CategoryModel> categoryModelList = categoryMapper.selectList(queryWrapper);
        for (int i = 0; i < categoryModelList.size(); i++) {
                int count1 = complaintService.selectIndustryCount(categoryModelList.get(i).getId(),user.getId(),user.getRoles());
                ValueAndNameVo vo = new ValueAndNameVo();
                vo.setName(categoryModelList.get(i).getName());
                vo.setValue(count1);
                list.add(vo);
        }
        return Result.success(list);
    }
}